<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>修改全局配置</title>
    <link rel="stylesheet" th:href="@{/pear/component/pear/css/pear.css}"/>
    <link rel="stylesheet" th:href="@{/pear/component/layui/css/layui.css}"/>
</head>
<body>
<form class="layui-form" method="POST" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="layui-form-item">
                <label class="layui-form-label">配置名</label>
                <div class="layui-input-block">
                    <input type="text" name="settingName" lay-verify="required" maxlength="20" autocomplete="off" placeholder="请输入配置名" class="layui-input" id="setting-name">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">配置key</label>
                <div class="layui-input-block">
                    <input type="text" name="settingKey" lay-verify="required" maxlength="50" placeholder="请输入配置key" class="layui-textarea">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">配置值</label>
                <div class="layui-input-block">
                    <textarea name="settingValue" lay-verify="required" maxlength="50" placeholder="请输入配置值" class="layui-textarea" id="setting-value"></textarea>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-success layui-btn-sm" lay-submit=""
                    lay-filter="setting-update">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script th:src="@{/pear/component/layui/layui.js}"></script>
<script th:inline="javascript">
    layui.use(['form', 'jquery', 'layer'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let layer = layui.layer;
        let baseURL = /*[[@{/}]]*/ '';
        // 自定义校验规则
        form.verify({
            // 验证配置名，且为必填项
            settingName: function (value) {
                if (value.length < 1 || value.length > 20) {
                    return '配置名长度应在1到20个字符之间';
                }
            },
            // 验证配置key，且为必填项
            settingKey: function (value) {
                if (!/^[A-Z][A-Z0-9_]*[A-Z0-9]$/.test(value)) {
                    return '配置key只能是大写字母和下划线组成，且下划线不能在开头或结尾';
                }
            },
            // 验证配置值，且为必填项
            settingValue: function (value) {
                if (value.length < 1 || value.length > 50) {
                    return '配置值长度应在1到50个字符之间';
                }
            }
        });
        // 获取 URL 中的参数（配置 ID）
        const urlParams = new URLSearchParams(window.location.search);
        const settingId = urlParams.get('settingId');
        // 提交修改
        form.on('submit(setting-update)', function (data) {
            data.field.settingId = settingId;
            $.ajax({
                url: baseURL + 'setting/update',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post', // 使用 PUT 方法更新
                success: function (result) {
                    if (result.code === 1000) { // 更新时检查状态码
                        layer.msg(result.msg, {
                            icon: 1,
                            time: 2000
                        }, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name)); // 关闭当前页
                            parent.layui.table.reload("config-table"); // 刷新表格
                        });
                    } else {
                        layer.msg(result.msg, {
                            icon: 2,
                            time: 2000
                        });
                    }
                },
                error: function (xhr, status, error) {
                    layer.msg('更新配置失败', {icon: 2});
                }
            });
            return false; // 阻止表单默认提交
        });
    });

</script>
</body>
</html>
